//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Page;
import model.Type;
import service.GoodsService;
import service.TypeService;

@WebServlet(
    name = "goods_List",
    urlPatterns = {"/goods_list"}
)
public class GoodsListServlet extends HttpServlet {
    private GoodsService gService = new GoodsService();
    private TypeService tService = new TypeService();

    public GoodsListServlet() {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = 0;
        if (request.getParameter("typeid") != null) {
            id = Integer.parseInt(request.getParameter("typeid"));
        }

        int pageNumber = 1;
        if (request.getParameter("pageNumber") != null) {
            try {
                pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
            } catch (Exception var7) {
            }
        }

        Type t = null;
        if (id != 0) {
            t = this.tService.selectTypeNameByID(id);
        }

        request.setAttribute("t", t);
        if (pageNumber <= 0) {
            pageNumber = 1;
        }

        Page p = this.gService.selectPageByTypeID(id, pageNumber);
        if (p.getTotalPage() == 0) {
            p.setTotalPage(1);
            p.setPageNumber(1);
        } else if (pageNumber >= p.getTotalPage() + 1) {
            p = this.gService.selectPageByTypeID(id, p.getTotalPage());
        }

        request.setAttribute("p", p);
        request.setAttribute("id", String.valueOf(id));
        request.getRequestDispatcher("/goods_list.jsp").forward(request, response);
    }
}
